package com.ruoyix.weblog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyix.common.utils.StringUtils;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyix.weblog.domain.bo.BlogTreeHoleBo;
import com.ruoyix.weblog.domain.vo.BlogTreeHoleVo;
import com.ruoyix.weblog.domain.BlogTreeHole;
import com.ruoyix.weblog.mapper.BlogTreeHoleMapper;
import com.ruoyix.weblog.service.IBlogTreeHoleService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 树洞Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-27
 */
@RequiredArgsConstructor
@Service
public class BlogTreeHoleServiceImpl implements IBlogTreeHoleService {

    private final BlogTreeHoleMapper baseMapper;

    /**
     * 查询树洞
     */
    @Override
    public BlogTreeHoleVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询树洞列表
     */
    @Override
    public TableDataInfo<BlogTreeHoleVo> queryPageList(BlogTreeHoleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogTreeHole> lqw = buildQueryWrapper(bo);
        Page<BlogTreeHoleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询树洞列表
     */
    @Override
    public List<BlogTreeHoleVo> queryList(BlogTreeHoleBo bo) {
        LambdaQueryWrapper<BlogTreeHole> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BlogTreeHole> buildQueryWrapper(BlogTreeHoleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlogTreeHole> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAvatar()), BlogTreeHole::getAvatar, bo.getAvatar());
        lqw.eq(StringUtils.isNotBlank(bo.getMessage()), BlogTreeHole::getMessage, bo.getMessage());
        return lqw;
    }

    /**
     * 新增树洞
     */
    @Override
    public Boolean insertByBo(BlogTreeHoleBo bo) {
        BlogTreeHole add = BeanUtil.toBean(bo, BlogTreeHole.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改树洞
     */
    @Override
    public Boolean updateByBo(BlogTreeHoleBo bo) {
        BlogTreeHole update = BeanUtil.toBean(bo, BlogTreeHole.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BlogTreeHole entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除树洞
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
